<?php
class ProjectSelectedTopics extends CActiveRecord {
	/**
	 * The followings are the available columns in table 'v_project_topic_tree':
	 * @var integer $id
	 * @var string $label
	 * @var string $description
	 * @var integer $parent_id
	 * @var string $step_mnemonic
	 * @var integer $orderIndex
	 * @var integer $project_id
	 * @var integer $project_topic_id
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'v_project_topic_tree';
	}

	public function primaryKey() {
		return array (
			'id',
			'project_id'
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		return array (
			array (
				'label',
				'length',
				'max' => 100
			),
			array (
				'step_mnemonic',
				'length',
				'max' => 5
			),
			array (
				'id, label, description, orderIndex, project_id',
				'required'
			),
			array (
				'id, parent_id, orderIndex, project_id, project_topic_id',
				'numerical',
				'integerOnly' => true
			),


		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array (
			'children' => array (
				self::HAS_MANY,
				'ProjectSelectedTopics',
				'parent_id,project_id',
				'order' => 'orderIndex desc'
			),
			'parent' => array (
				self::BELONGS_TO,
				'ProjectSelectedTopics',
				'parent_id,project_id'
			),
			'step' => array (
				self::BELONGS_TO,
				'ProjectSelectedSteps',
				'step_id,project_id'
			),
			'topic' => array (
				self::BELONGS_TO,
				'ProjectTopic',
				'project_topic_id'
			)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array (
			'id' => 'Id',
			'label' => 'Label',
			'description' => 'Description',
			'parent_id' => 'Parent',
			'step_mnemonic' => 'Step Mnemonic',
			'orderIndex' => 'Order Index',
			'project_id' => 'Project',
			'project_topic_id' => 'Project Topic',


		);
	}

	private $_canBeObjective = null;

	public function canBeObjective() {
		if ($this->_canBeObjective === null) {
			$this->_canBeObjective = false;
			if (isset ($this->children) && $this->children != null && count($this->children) > 0) {
				$queue = new Queue();
				$queue->enqueue_all($this->children);
				while (!$queue->is_empty() && !$this->_canBeObjective) {
					$topic = $queue->dequeue();
					if ($topic->canBeObjective()) {
						$queue->clear();
						$this->_canBeObjective = true;
					}
				}
			}
			elseif (!$this->topic->adviser_validation || $this->topic->adviser_validation == 0) {
				$this->_canBeObjective = true;
			}
		}

		return $this->_canBeObjective;
	}
}